import React from 'react';
import {connect} from 'dva';
import {Link, browserHistory} from 'dva/router';
import {Table, Row, Col, Form, Input, Icon, InputNumber, Radio, DatePicker, Select, Upload, Button, Switch, Modal, Popconfirm, Tabs, message} from 'antd';
const R = require('ramda');
import moment from 'moment'
const dateFormat = 'YYYY-MM-DD';
const { RangePicker } = DatePicker;
import RightContacts from '../../../utils/RightConstants';
import CustomerDetail from '../../../components/supplier/components/CustomerDetail';
import OrderDetail from '../../../components/order/components/OrderDetail';
import RightBox from "../../../components/commons/RightBox";
import Constants from "../../../utils/Constants";
const FormItem = Form.Item;
const Option = Select.Option;
const RadioGroup = Radio.Group;
export default connect(({customerManage, drawer, orderManage, businessAgreement, settlement, capital, customerReport, blacklist, customs }) => ({customerManage, drawer, orderManage, businessAgreement, settlement, capital, customerReport, blacklist, customs }))(
    Form.create()(class customerDetails extends React.Component {
        constructor(props) {
            super(props);
            this.state = {
            }
        }

        componentDidMount() {
            this.props.dispatch({
                type: 'customerManage/queryCustomerManageList',
                payload: {
                    status: 4,
                    businessType: Constants.NUMBER_ONE
                }
            });
        }

        handleResetFields = () => {
            if(this.editCustomer)this.editCustomer.handleResetFields();
        }

        handleCancel = (flag) => {
            if(flag){
                this.handleResetFields();
            }
        }

        onView = (record) => {
            this.handleOpenDetail(record, false,  {}, "1", true);
        }


        handleOpenDetail = (record, customerDisabled, searchConditions, tabActiveKey, editCustomerDisable)  => {
            this.props.dispatch({
                type: 'customerManage/getCustomerById', payload: {
                    id: record.id, isApproval: false,
                    onSuccess: (customer) => {
                        let  isblacklist =false;
                        if (customer.isblacklist) {
                            isblacklist = true;
                        }

                        this.props.dispatch({type: 'customerManage/getLinkmanList', payload: {cstId: record.id, page :  this.props.customerManage.linkmanPage}});//查询联系人列表
                        this.props.dispatch({type: 'customerManage/getRecAccList', payload: {cstId: record.id, page: this.props.customerManage.recAccPage}});
                        this.props.dispatch({type: 'customerManage/getProductList', payload: {cstId: record.id, page: this.props.customerManage.productPage}});
                        this.props.dispatch({type: 'businessAgreement/getAgreementlList', payload: {cstid: record.id, status: 1}});
                        this.props.dispatch({type: 'settlement/queryStatements', payload: { stObjId: record.id, status: 2, page: Constants.DEFAULT_PAGE }});
                        this.props.dispatch({ type: 'orderManage/queryOrderManageList', payload: { supplierId: record.id, page: Constants.DEFAULT_PAGE, status: 1 } });
                        // this.props.dispatch({type: 'customerReport/getReconciliationList', payload: {cstId: record.id}});
                        // this.props.dispatch({type: 'customerReport/getOrderBilldetailList', payload: {cstId: record.id}});

                        this.refs.customerDetail.handleOpen(customerDisabled, searchConditions, tabActiveKey, record.id, editCustomerDisable, "customerDetail");
                    }
                }
            });

        }


        handleAddOrder = (record) =>{
            if(!record.manager || !record.waiter || !record.tgmanager){
                message.error("请确认跟单经理、采购经理、通关经理是否都已经全部分配完毕！");
                return;
            }
            this.props.dispatch({ type: 'orderManage/queryLinkmanList', payload: { cstId: record.id } });//查询联系人列表
            this.refs.orderDetail.handleOpen(false, false, false, false, {}, "1", record.id);
        }

        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.customerManage.page;
            newPage.page = 1;
            let time=values.time;
            this.props.dispatch({
                type: 'customerManage/queryCustomerManageList',
                payload: {
                    customerNO: values.customerNO,
                    cstName: values.cstName,
                    cstManage: values.cstManage,
                    cstWaiter: values.cstWaiter,
                    beginTime:R.isNil(time)||R.isEmpty(time)?null:time[0].format(dateFormat),
                    endTime:R.isNil(time)||R.isEmpty(time)?null:time[1].format(dateFormat),
                    status: 4,
                    businessType: Constants.NUMBER_ONE,
                    page: newPage
                }
            });
        };



        columns = [
            {title: '供应商编码', dataIndex: 'customerno', key: 'customerno'},

            {title: '商品供应商',
                render(record){
                    return record.companynameen + "(" + record.companyname + ")"
                }
            },
            {title: '供应商类型', render(record) {
                if(record.companytype === "0"){
                    return "生产型企业";
                }else{
                    return "贸易公司";
                }
            }},
            {title: '采购经理', render(record) {
                if(R.isNil(record.managerName) || R.isEmpty(record.managerName)){
                    return "未分配";
                }else{
                    return record.managerName;
                }
            }},

            {title: '跟单经理', render(record) {
                if(R.isNil(record.waiterName) || R.isEmpty(record.waiterName)){
                    return "未分配";
                }else{
                    return record.waiterName;
                }
            }},

            {title: '创建时间', dataIndex: 'createtime', key: 'createtime'},

            {title: '操作',  key: 'key_oprator',

                render: (text, record, index) => {
                    return (
                        <span>
                            <RightBox resource={RightContacts.SUPPLIER.SUPPLIER_MANAGE_INFOMAINTENANCE} split={true}><a href="#" onClick={this.onView.bind(this, record)}>信息维护</a></RightBox>
                            <RightBox resource={RightContacts.SUPPLIER.SUPPLIER_MANAGE_ADDORDER}>
                                <a href="#" onClick={this.handleAddOrder.bind(this, record)}>添加订单</a>
                            </RightBox>
                        </span>
                    );
                }
            }
        ];

        render () {
            let dataSouce = R.isNil(this.props.customerManage.dataSource) ? {} : this.props.customerManage.dataSource;
            let formItemLayout = {labelCol: {span: 6}, wrapperCol: {span: 18}};
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            let time=values.time;
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                pageSize: this.props.customerManage.page.size,
                total: this.props.customerManage.page.totalRow,
                showSizeChanger: true,
                current: this.props.customerManage.page.page,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.customerManage.page;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'customerManage/queryCustomerManageList',
                        payload: {
                            customerNO: values.customerNO,
                            cstName: values.cstName,
                            cstManage: values.cstManage,
                            cstWaiter: values.cstWaiter,
                            beginTime:R.isNil(time)||R.isEmpty(time)?null:time[0].format(dateFormat),
                            endTime:R.isNil(time)||R.isEmpty(time)?null:time[1].format(dateFormat),
                            status: 4,
                            businessType: Constants.NUMBER_ONE,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.customerManage.page;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'customerManage/queryCustomerManageList',
                        payload: {
                            customerNO: values.customerNO,
                            cstName: values.cstName,
                            cstManage: values.cstManage,
                            cstWaiter: values.cstWaiter,
                            beginTime:R.isNil(time)||R.isEmpty(time)?null:time[0].format(dateFormat),
                            endTime:R.isNil(time)||R.isEmpty(time)?null:time[1].format(dateFormat),
                            status: 4,
                            businessType: Constants.NUMBER_ONE,
                            page: newPage
                        }
                    });
                }
            }
            return (
                <div>

                    <Row>
                        <Col span={7}><FormItem {...formItemLayout} label={'供应商编码'}>{getFieldDecorator('customerNO')(<Input/>)}</FormItem></Col>
                        <Col span={7}><FormItem {...formItemLayout} label={'商品供应商'}>{getFieldDecorator('cstName')(<Input/>)}</FormItem></Col>
                    </Row>
                    <Row>
                        <Col span={7}><FormItem {...formItemLayout} label={'采购经理'}>{getFieldDecorator('cstManage')(<Input/>)}</FormItem></Col>
                        <Col span={7}><FormItem {...formItemLayout} label={'跟单经理'}>{getFieldDecorator('cstWaiter')(<Input/>)}</FormItem></Col>
                    </Row>
                    <Row>
                        <Col span={7}>
                            <FormItem {...formItemLayout} label={'创建时间'}>
                                {getFieldDecorator('time', {})(
                                    <RangePicker
                                        style={{width: '100%'}}
                                        showTime={{ format: 'YYYY-MM-DD' }}
                                        format="YYYY-MM-DD"
                                        placeholder={['开始时间', '结束时间']}
                                    />
                                )}
                            </FormItem>
                        </Col>
                        <Col span={7} style={{float: 'right'}}>
                            <Button type="primary" icon="search" style={{marginRight: 5}} onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>

                    </Row>

                    <Table dataSource={this.props.customerManage.suppilerList} columns={this.columns} bordered={true} size={'middle'} pagination={pagination}/>

                    <CustomerDetail ref={"customerDetail"} cstId={dataSouce.id} drawers={this.props.drawer} {...this.props.customerManage} customs={this.props.customs}
                                    orderManage = {this.props.orderManage}
                                    businessAgreement = {this.props.businessAgreement}
                                    settlement = {this.props.settlement}
                                    dispatch={this.props.dispatch} showPA ={true}
                                    capital = {this.props.capital}
                                    customerReport = {this.props.customerReport}
                                    blacklist = {this.props.blacklist}
                                    handleSearch = {this.handleSearch}/>
                    
                    <OrderDetail ref={"orderDetail"} {...this.props.orderManage} {...this.props.customerManage} dispatch={this.props.dispatch} refreshList={this.handleSearch} />

                </div>
            )
        }
    })


)
